package com.hjl.servlet;

import com.hjl.entity.User;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author ServivE
 * @version 0.1
 * @className OnLineUsersServlet
 * @description
 * @date 2024/7/26 20:26
 * @since jdk11
 */
@WebServlet("olUsers1")
public class OnLineUsersServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建ServletContext application 对象取出集合
        ServletContext application = request.getServletContext();
        List<User> onLineUsers = (List<User>) application.getAttribute("onLineUsers");
        //输出展示
        response.setContentType("html/text;charset=UTF-8");
        PrintWriter out = response.getWriter();
        for (User u : onLineUsers) {
            out.println(u.getId() + "," + u.getUsername() + "<br/>");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
